<?php
/**
 * Abstract base for DAO
 * 
 * @author Mitchelle Pascual <mitch.pascual@gmail.com>
 * $date 04/01/2010
 */
abstract class Owg_Model_BaseDAO
{
	const SQL_CONDITION_EQUALS 		= '=';
	const SQL_CONDITION_NOT_EQUALS 	= '<>';
	const SQL_CONDITION_IN 			= 'IN';
	const SQL_CONDITION_NOT_IN 		= 'NOT IN';
	const SQL_CONDITION_BETWEEN		= 'BETWEEN';
	const SQL_OPERATOR_AND 			= 'AND';
	const SQL_OPERATOR_OR 			= 'OR';
	
	protected $_dbTable;
	
 	public function setDbTable($dbTable)
    {
        if (is_string($dbTable)) {
            $dbTable = new $dbTable();
        }
        if (!$dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception('Invalid table data gateway provided');
        }
        $this->_dbTable = $dbTable;
        return $this;
    }

    public function getDbTable()
    {
    	if (null === $this->_dbTable || !$this->_dbTable instanceof Zend_Db_Table_Abstract) {
            $this->setDbTable($this->_dbTable);
        }

        return $this->_dbTable;
    }
	
	// we don't wan't these in the public interace so we'll put it here    
  	abstract protected function _getWhereClause($p_sFilterField, $p_mFilterValue = '', 
  		$p_sFilterCondition = Owg_Model_BaseDAO::SQL_CONDITION_EQUALS, $p_sFilterOperator = '');
   	abstract protected function _getOrderClause($p_sOrder, $p_bAsc = true);
    
    
    /**
     * Extract multiple where clauses
     * 
     * @param array $p_amWhereClauses 
     * eg. array(array('field' => '', 'value' => '', 'condition' => '', 'operator' => '');
     */
    protected function extractWhereClauses($p_amWhereClauses = array())
    {
    	$sWhereClause = '';
    	
    	foreach ($p_amWhereClauses as $aWhereClause)
    	{
    		if (!isset($aWhereClause['field']) || !isset($aWhereClause['value'])
    			|| !isset($aWhereClause['condition']) || !isset($aWhereClause['operator']) )
    			{
    				throw new Exception('extractWhereClauses exception. check array format.');
    			}

    			$sWhereClause .= $this->_getWhereClause($aWhereClause['field'], 
    				$aWhereClause['value'], $aWhereClause['condition'], $aWhereClause['operator']);
    		
    	}
    	
    	return $sWhereClause;
    	
    }
    
    
    
}